<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">楼宇名称：</div>
      <el-input v-model="params.name" clearable placeholder="请输入内容" class="search-main" @clear="getList"
        style=" width: 45%" />
      <el-button type="primary" @click="getList">查询</el-button>
    </div>
    <!-- 添加楼宇 -->
    <div class="addcontainer">
      <el-button type="primary" round style="height: auto;" @click="open">添加楼宇</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="list" style=" width: 100%">
        <!-- 序号列 -->
        <el-table-column type="index" label="序号" />
        <!-- 楼宇名称列 -->
        <el-table-column label="楼宇名称" width="180" prop="name" />
        <!-- 层数列 -->
        <el-table-column label="层数" prop="floors" />
        <!-- 在管面积列 -->
        <el-table-column label="在管面积(m²)" width="120" prop="area" />
        <!-- 物业费列 -->
        <el-table-column label="物业费(元/m²)" width="120" prop="propertyFeePrice" />
        <!-- 状态列 -->
        <el-table-column label="状态" prop="status" :formatter="fn" />
        <!-- 操作列 -->
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页区域 -->
    <div class="page-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="params.page"
        :page-sizes="[2, 4, 6, 8, 10]" :page-size="params.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!-- 编辑/添加楼宇对话框 -->
    <el-dialog :visible="flag" @close="closeDialog" :title="form.id ? '编辑楼宇' : '新增楼宇'">
      <el-form ref="form" :model="form" :rules="rules">
        <!-- 楼宇名称输入框 -->
        <el-form-item label="楼宇名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <!-- 层数输入框 -->
        <el-form-item label="楼宇层数" prop="floors">
          <el-input v-model="form.floors"></el-input>
        </el-form-item>
        <!-- 在管面积输入框 -->
        <el-form-item label="在管面积" prop="area">
          <el-input v-model="form.area"></el-input>
        </el-form-item>
        <!-- 物业费输入框 -->
        <el-form-item label="物业费" prop="propertyFeePrice">
          <el-input v-model="form.propertyFeePrice"></el-input>
        </el-form-item>
        <!-- 确认和取消按钮 -->
        <el-form-item>
          <el-button type="primary" round @click="confirm">确定</el-button>
          <el-button type="success" round @click="closeDialog">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import { getBuildingListAPI } from '@/api/building'
import { delBuildingAPI } from '@/api/building'
import { addBuildingAPI } from '@/api/building'
import { editBuildingAPI } from '@/api/building'
export default {
  name: 'Building',

  data() {
    return {
      // 控制编辑/添加楼宇对话框的显示隐藏
      flag: false,
      // 楼宇列表数据
      list: [],
      // 总数据量
      total: 0,
      // 查询参数
      params: {
        page: 1,
        pageSize: 6,
        name: '',
      },
      // 编辑/添加楼宇的表单数据
      form: {
        name: '',
        floors: '',
        area: '',
        propertyFeePrice: ''
      },
      // 表单校验规则
      rules: {
        name: [
          { required: true, message: '必须填写', trigger: 'blur' }
        ],
        floors: [
          { required: true, message: '必须填写', trigger: 'blur' }
        ],
        area: [
          { required: true, message: '必须填写', trigger: 'blur' }
        ],
        propertyFeePrice: [
          { required: true, message: '必须填写', trigger: 'blur' }
        ]
      }
    }
  },

  mounted() {
    // 页面加载时调用获取列表数据的方法
    this.getList()
  },
  methods: {
    // 编辑楼宇信息
    edit(row) {
      console.log(row)
      // 打开弹窗
      this.flag = true

      // 将要编辑的楼宇信息回显到表单中
      this.form.name = row.name
      this.form.floors = row.floors
      this.form.area = row.area
      this.form.propertyFeePrice = row.propertyFeePrice
      this.form.id = row.id//将来编辑需要的id
    },

    // 点击确认按钮提交表单数据
    confirm() {
      // 校验表单数据
      this.$refs.form.validate((ok) => {
        if (ok) {
          if (this.form.id) {
            console.log('编辑')
            // 调用编辑楼宇信息的 API
            editBuildingAPI(this.form).then(() => {
              this.$message.success('编辑成功！')
              // 刷新列表数据
              this.getList()
              // 关闭对话框
              this.flag = false
              // 编辑完成以后清空数据，删除id字段
              this.form = {
                name: '',
                floors: '',
                area: '',
                propertyFeePrice: ''
              }
            }).catch(() => {
              // this.$message.error(error.response.data.msg)
              this.$message.error('编辑失败！')
            })
          } else {
            // 调用添加楼宇信息的 API
            addBuildingAPI(this.form).then(() => {
              this.$message.success('添加成功！')
              // 刷新列表数据
              this.getList()
              // 关闭对话框
              this.flag = false
              // 清空表单数据
              this.form = {
                name: '',
                floors: '',
                area: '',
                propertyFeePrice: ''
              }
            }).catch(() => {
              // this.$message.error(error.response.data.msg)
              this.$message.error('添加失败！')
            })
          }
        }
      })
    },

    // 关闭编辑/添加楼宇对话框
    closeDialog() {
      this.flag = false
      // 清空表单数据
      this.form = {
        name: '',
        floors: '',
        area: '',
        propertyFeePrice: ''
      }
    },

    // 获取楼宇列表数据
    getList() {
      getBuildingListAPI(this.params).then((res) => {
        this.list = res.data.rows,
          this.total = res.data.total
      })
    },

    // 点击添加楼宇按钮，打开编辑/添加楼宇对话框
    open() {
      this.flag = true
    },

    // 分页相关方法
    handleSizeChange(pageSize) {
      this.params.page = 1,
        this.params.pageSize = pageSize,
        this.getList()
    },
    handleCurrentChange(page) {
      this.params.page = page
      this.getList()
    },

    // 状态列格式化方法
    fn(row) {
      return row.status === 0 ? '空置中' : '租赁中'
    },

    // 删除楼宇信息
    del(id) {
      this.$confirm('你确定要删除吗？').then(() => {
        delBuildingAPI(id).then(() => {
          // 刷新列表数据
          this.getList()
          this.$message.success('删除成功！')
        }).catch(() => {
          this.$message.error('删除失败')
        })
      })
    }
  }
}

</script>
<style lang="scss" scoped>
.department-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>
